<?php 
## include required files
/*******************************/
require_once '../model/users.php';
require_once '../model/email.php';
require_once '../model/admindetails.php';
/*******************************/

## Create Objects
/*******************************/
$userObj = new Model_Users();
$emailObj = new Model_Email();
$adminObj = new Model_AdminDetails();
/*******************************/
if(isset($_POST['adduser_btn'])) {
	## apply PHP validation for required filed
	if(trim($_POST['username']) != '' && trim($_POST['first_name']) != '' && trim($_POST['last_name']) != '' && trim($_POST['email']) != '') {
		## Check is username exist
		$username_result = $adminObj->chkUserNameExist(trim($_POST['username']));
		## Check is email address exist
		$email_result = $adminObj->chkUserEmailExist(trim($_POST['email']));
		
		if(count($username_result) > 0) {
			$_SESSION['msg'] = '<div class="error_msg"><span>Username already exist</span></div>';		
		} else if(count($email_result) > 0) {
			$_SESSION['msg'] = '<div class="error_msg"><span>Email already exist</span></div>';		
		} else {
				$userArray = array();
				extract($_POST);
				
				## Generate Password
				$pass = rand(999999, 2);
				
				$userArray['username']   = return_post_value($username);
				$userArray['first_name']   = return_post_value($first_name);
				$userArray['last_name']   = return_post_value($last_name);
				$userArray['email']   = return_post_value($email);
				$userArray['password']   = md5($pass);
				$userArray['user_type']   = '3';
				$userArray['registered_date']   = getCurDate();
				$userid = $userObj->addUserByValue($userArray);
				
				// Send email to admin user login details
					## Fetch email content
					$emailArray = $emailObj->getEmailById(4);
					$subject = $emailArray['email_subject'];
					$subject = str_replace('[SITENAME]', SITENAME, $subject);
					$message = $emailArray['email_content'];
					
					$link= '<a href="'.SITE_URL."/admin/activation.php?id=".md5($userid).'">'.SITE_URL."/admin/activation.php?id=".md5($userid).'</a>';
					
					## Create message
					$message = str_replace('[NAME]', $userArray['first_name']." ".$userArray['last_name'], $message);
					$message = str_replace('[USERNAME]',$userArray['username'] , $message);
					$message = str_replace('[PASSWORD]',$pass , $message);
					$message = str_replace('[SITENAME]', SITENAME, $message);
					$message = str_replace('[ACTIVATIONURL]', $link, $message);
					$message = str_replace('[SITELINK]',SITE_URL , $message);
					
					$to = $userArray['email'];
					
					$from = $emailArray['from_email'];
					$fromname = FRONTTITLE;
					
					$emailTemplate = file_get_contents('../emailtemplate.html');
					
					$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
					$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/templates/default/images/logo.png">', $template_msg);
					$template_msg = str_replace('[MESSAGE]',$message , $template_msg);
		
					if(trim($to) != '')
						@send_mail($to, $template_msg, $subject, $from, $fromname);
						
					$_SESSION['msg'] = "<div class='success_msg'><span>User added successfully</span></div>";
					header('location:'.SITE_URL.'/admin/home.php?q=manageusers');
					exit;
		}
	} else { // php validation else
		$_SESSION['msg'] = '<div class="error_msg"><span>Please fill all required fields</span></div>';
	}
	// main if closed
}

## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu
$smarty->assign('activeclass', 'manageusers');
$smarty->assign('mainmenu', '3');
## Unset all the objects created which are on this page
unset($userObj);
unset($emailObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'users/addnewuser.tpl');
unset($smarty);
?>